package com.gohi.main.store.action;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.ParseException;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

//import com.caucho.server.http.HttpRequest;
import com.gohi.core.web.action.BaseAction;
import com.gohi.main.store.hbm.ShopgooBeautician;
import com.gohi.main.store.hbm.ShopgooComment;
import com.gohi.main.store.hbm.ShopgooGroupbuy;
import com.gohi.main.store.hbm.ShopgooSpecialoffer;
import com.gohi.main.store.hbm.ShopgooStore;
import com.gohi.main.store.service.ICommentService;
import com.gohi.main.store.service.IDiscountService;
import com.gohi.main.store.service.IStoreService;
import com.gohi.main.system.hbm.ShopgooArea;
import com.gohi.main.system.service.ISystemService;
import com.gohi.main.user.hbm.ShopgooMember;
import com.gohi.main.util.AppJsonUtils;
import com.gohi.main.util.StringUtil;
import com.gohi.main.util.Utils;
import com.gohi.main.util.WebUtils;
import com.gohi.main.util.Cache.MainCache;

import com.gohi.main.wxapi.util.AccoutUtil;
import com.gohi.main.wxapi.vo.JsApi;
import com.gohi.main.wxapi.vo.WxAccount;

/**
 * 折扣相关
 * 
 * @author Administrator
 * 
 */
public class DiscountAction extends BaseAction {
	Logger logger = Logger.getLogger(DiscountAction.class);
	//注入特惠信息
	private IDiscountService discountService;
	private  ShopgooSpecialoffer sp;
	public String index() throws IOException{
		if(this.getSession().getAttribute("openid")==null){
			getSession().setAttribute("openid", "olkcGj2C5XaVRl95RQlDe5aFJwqM");
		}
		if(sp==null){
			sp =new ShopgooSpecialoffer();
		}
		
		//初始化城市
		Cookie ck = WebUtils.getCookieByName(getRequest(), "city");
		if (ck == null) {
			sp.setCity_id(67);// 默认上海
			WebUtils.addCookie(getResponse(), "city", "67-上海", -1);// 默认上海
		} else {
			sp.setCity_id(Integer.parseInt(ck.getValue().split("-")[0]));// 默认上海
		}
		
		
		
		
		String type = getRequest().getParameter("type");// 标示是否AJAX请求来的
		if (type != null && type.equals("ajax")) {
			// System.err.println(group.getLimit());
			List list= discountService.queryDiscount(sp);
			AppJsonUtils.ptinrJson(list, getRequest(), getResponse());
			return null;
		}
		
		List list= discountService.queryDiscount(sp);
		jsonString =  AppJsonUtils.getJson(list); 
		//System.err.println(discountService.queryDiscount(sp).size());
		return "index";
	}
	
	public String detail() throws ParseException{
		String id  =this.getRequest().getParameter("id");
		ShopgooSpecialoffer group = discountService.discountById(id);
		if(group.getEnd_time()<=StringUtil.getTimeSimple()){
			this.getRequest().setAttribute("isend", "1");
		}
		this.getRequest().setAttribute("group", group);
		
	
		// 计算默认的时间

		String text = StringUtil.splitTime(StringUtil.parentSimple(group.getBusiness_starttime(), "HH:mm")
				, StringUtil.parentSimple(group.getBusiness_endtime(), "HH:mm"),group.getHowlong());
		String text1 = StringUtil.splitTime(StringUtil.parentSimple(group.getBusiness_starttime(), "HH:mm")
				, StringUtil.parentSimple(group.getBusiness_endtime(), "HH:mm"),group.getHowlong(),0);
		
		
		//System.err.println(text);
		Map<String, String> map = new LinkedHashMap<String, String>();
		String d = StringUtil.getDate("yyyy-MM-dd");
		// 循坏四天
		for (int i = 0; i <= 3; i++) {
			String key = StringUtil.getNextDate(d, i);
			if (!map.containsKey(key)) {
				if(i==0){
					//第一个表示今天 要对时间进行处理
					map.put(key, text1);
				}else{
					map.put(key, text);
				}
		
			}
		}
		// 转成JSON到客户端渲染
		JSONObject js = JSONObject.fromObject(map);
		this.jsonString = js.toString();
		
		
		
		return "detail";
	}
	
	public IDiscountService getDiscountService() {
		return discountService;
	}
	public void setDiscountService(IDiscountService discountService) {
		this.discountService = discountService;
	}


	public ShopgooSpecialoffer getSp() {
		return sp;
	}


	public void setSp(ShopgooSpecialoffer sp) {
		this.sp = sp;
	}
	
	
	
	
	
}
